package com.tq.utils;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/*Description:
 *this VerifyCode has 4 member，
 *it contain (A-Z,a_z,0-9),
 *and they have different color,
 *different location
 */

/*
 @how to use:
 @see  the verfycode.xml
 */
//问题：如何设置View的大小，目前是在布局文件中设置的
//干扰线为画上去
public class VerifyCodeView extends View {
	
	String code;
	
	Paint mPaint;

	private final int TEXT_SIZE = 30;

	public VerifyCodeView(Context context) {
		super(context);
		init();
	}

	public VerifyCodeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public VerifyCodeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {

		mPaint = new Paint();

		/* Sawtooth */

		mPaint.setAntiAlias(false);

		/* Set the frame width of the paint */

		mPaint.setStrokeWidth(4);

		// set the font size
		mPaint.setTextSize(TEXT_SIZE);
		
		// random fontsize
		// int fontSize = (int) Math.round(Math.random() * 4 + 16);

		mPaint.setTextAlign(Paint.Align.CENTER);

	}

	protected void onDraw(Canvas canvas) {

		/* set backgroud clolr */

		canvas.drawColor(Color.GRAY);

		canvas.save();

		canvas.translate(10, 10);// 参数的意思是原来的原点分别在x轴和y轴偏移多远的距离

		String sRand = "";

		for (int i = 0; i < 4; i++) {// 4 member
			/* set paint color */

			/* random color */
			// mPaint.setColor(Color.BLUE);
			mPaint.setColor(getRandomColor());
			/* random A-Z,a_z,0-9 */
			// String rand = String.valueOf(random.nextInt(10));//only int
			String rand = getRandomChar();
			/* random local */// height need to set byyourself
			int height = (int) (10 + Math.round(Math.random() * 15));
			canvas.drawText(rand, TEXT_SIZE * i + 4, height, mPaint);// 20
			// radom line unfinished
			// canvas.drawLine(0, 0, 20, 20, mPaint);
			sRand += rand;

		}

		saveCode(sRand);//将验证码的值保存下来

		canvas.restore();

	}

	/**
	 * random color return (int)
	 */
	public int getRandomColor() {
		Random random = new Random();
		int a, r, g, b;
		a = random.nextInt(255);
		r = random.nextInt(255);
		g = random.nextInt(255);
		b = random.nextInt(255);
		return Color.argb(a, r, g, b);
	}

	/**
	 * random A-Z,a_z,0-9
	 * 
	 * @createDate 2010-8-23 10:33:55
	 * @author hoojo
	 * @return
	 */
	public static String getRandomChar() {
		int index = (int) Math.round(Math.random() * 2); // Math.random()(0.0-1.0)
		String randChar = "";
		switch (index) {
		case 0:// return a-z
			randChar = String
					.valueOf((char) Math.round(Math.random() * 25 + 65));
			break;
		case 1:// return a-z
			randChar = String
					.valueOf((char) Math.round(Math.random() * 25 + 97));
			break;
		default:// return 0-9
			randChar = String.valueOf(Math.round(Math.random() * 9));
			break;
		}
		return randChar;
	}

	/**
	 * <b>function:</b> 随机生成字体、文字大小
	 * 
	 * @createDate 2010-8-23 上午10:44:22
	 * @author hoojo
	 * @return
	 */
//	public static Font getRandomFont() {
//		String[] fonts = { "Georgia", "Verdana", "Arial", "Tahoma",
//				"Time News Roman", "Courier New", "Arial Black", "Quantzite" };
//		int fontIndex = (int) Math.round(Math.random() * (fonts.length - 1));
//		int fontSize = (int) Math.round(Math.random() * 4 + 16);
////		return new Font(fonts[fontIndex], Font.PLAIN, fontSize);
//	}

	/**
	 * Save coding in here
	 * 
	 * @param code
	 */

	private void saveCode(String code) {
       this.code = code;
		// TODO

	}
	//取得验证码
    public String getCode(){
    	return code;
    }
}
